import java.io.*;
import java.util.*;

public class find {

    public static StringBuffer help () {
	StringBuffer sbf = new StringBuffer();
	sbf.append("==========================================================================\n");
	sbf.append("Usage:java find [1] [directory] [file type (.java)] [Search String]\n");
	sbf.append("Usage:java find [2] [directory] [file type (.java)] [String1] [String2]\n");
	sbf.append("===========================================================================\n");
	return sbf;
    }

    public static void main  (String[] args) {
	if(args == null || args.length <4) {
	    System.out.println(help());
	    System.exit(0);
	}

	String dir            = null;
	String file_type      = null;
	String current_string = null;
	String new_string     = null;

	int which = Integer.parseInt(args[0]);
	Files fs = null;
	Vector vfs = null;
	Enumeration enums = null;
	File f = null;

	if(which == 1) {
	    if(args.length != 4) {
		System.out.println(help());
		System.exit(0);
	    }

	    if(!args[2].startsWith(".")) {
		System.out.println(help());
		System.exit(0);
	    }

	    dir            = args[1];
	    file_type      = args[2];
	    current_string = args[3];
	    fs = new Files(dir, file_type);
	    vfs = fs.getFiles();
	    enums = vfs.elements();

	    StringBuffer rbuf = null;
	    Searcher search = new Searcher();

	    while(enums.hasMoreElements()) {
		search.setFile((String)enums.nextElement(), current_string);
		rbuf = search.getResult();
		
		if(rbuf != null) {
		    System.out.println(rbuf);
		}
	    }
	}

	if(which == 2) {
	    if(args.length != 5) {
		System.out.println(help());
		System.exit(0);
	    }


	    if(!args[2].startsWith(".")) {
		System.out.println(help());
		System.exit(0);
	    }

	    dir            = args[1];
	    file_type      = args[2];
	    current_string = args[3];
	    new_string     = args[4];

	    fs = new Files(dir, file_type);
	    vfs = fs.getFiles();

	    enums = vfs.elements();
	    
	    ReplaceString rs = new ReplaceString();
	    
	    while(enums.hasMoreElements()) {
		rs.setAttributes((String)enums.nextElement(), current_string, new_string);
		System.out.println(rs.replace());
	    }
	}
    }
}



